<?php

/*
 * класс написан just for fun. использовать в серьезных целях не надо=) 
 */

class HtmlRendering
{
	private static $_is_tabulation_enabled = true;
	private static $_nesting_level = 0;
	private static $_tab = "   ";

	protected static $_EOL = "\n";
	protected static $_ccs_dir = "";
	protected static $_js_dir = "";

	public static function __callStatic($name, $attrs = null)
	{
		if (substr($name, 0, 1) == "_")
		{
			$name = substr($name, 1);
			
			self::render_tabs(-1);

			echo "</{$name}>".self::$_EOL;
		}
		elseif (($dec_len = (strlen($name) - 1)) && (substr($name, $dec_len, 1) == "_"))
		{
			$name = substr($name, 0, $dec_len);
			
			self::render_tabs(1);

			echo "<{$name}";
			self::render_attrs($attrs[0]);
			echo ">".self::$_EOL;
		}
		else
		{
			self::render_tabs();
			
			echo "<{$name}";
			self::render_attrs($attrs[0]);
			echo ">$attrs[1]</{$name}>".self::$_EOL;
		}
	}

	protected static function render_attrs($attrs = null)
	{
		$attr_str = "";
		
		if (is_array($attrs))
		{
			foreach($attrs as $name => $value)
			{
				$attr_str .= " {$name}=\"{$value}\"";
			}
		}
		else
		{
			$attr_str = $attrs;
		}
		
		echo $attr_str;
	}
	
	protected static function render_tabs($level = 0)
	{
		if (!self::$_is_tabulation_enabled) return;

		if ($level < 0) self::$_nesting_level += $level;
		
		for($i = 0; $i < self::$_nesting_level; $i++)
		{
			echo self::$_tab;
		}
		
		if ($level > 0) self::$_nesting_level += $level;
	}
	
	public static function enable_tabulation($tab = null)
	{
		self::$_is_tabulation_enabled = true;
		
		if (!is_null($tab))
		{
			self::$_tab = $tab;
		}
	}
	
	public static function disable_tabulation()
	{
		self::$_is_tabulation_enabled = false;
	}
	
	public static function set_eol($eol = "\n")
	{
		self::$_EOL = $eol;
	}
	
	public static function set_default_eol()
	{
		self::$_EOL = "\n";
	}
	
	public static function set_css_dir($css_dir)
	{
		self::$_ccs_dir = $css_dir;
	}
	
	public static function set_js_dir($js_dir)
	{
		self::$_js_dir = $js_dir;
	}
}

class ExtendedHtmlRendering extends HtmlRendering
{
	static function div()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<div";
		self::render_attrs($attrs[0]);
		echo ">$attrs[1]</div>".self::$_EOL;
	}
	
	static function div_()
	{
		self::render_tabs(1);
		
		$attrs = func_get_args();
		
		echo "<div";
		self::render_attrs($attrs[0]);
		echo ">".self::$_EOL;
	}

	static function _div()
	{
		self::render_tabs(-1);
		
		echo "</div>".self::$_EOL;
	}
	
	static function a()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<a";
		self::render_attrs($attrs[0]);
		echo ">$attrs[1]</a>".self::$_EOL;
	}
	
	static function a_()
	{
		self::render_tabs(1);
		
		$attrs = func_get_args();
		
		echo "<a";
		self::render_attrs($attrs[0]);
		echo ">".self::$_EOL;
	}

	static function _a()
	{
		self::render_tabs(-1);
		
		echo "</a>".self::$_EOL;
	}
	
	static function b()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<b";
		self::render_attrs($attrs[0]);
		echo ">$attrs[1]</b>".self::$_EOL;
	}
	
	static function i()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<i";
		self::render_attrs($attrs[0]);
		echo ">$attrs[1]</i>".self::$_EOL;
	}
	
	static function input()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<input";
		self::render_attrs($attrs[0]);
		echo " />".self::$_EOL;
	}
	
	static function img()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<img";
		self::render_attrs($attrs[0]);
		echo " />".self::$_EOL;
	}
	
	static function clean($text)
	{
		self::render_tabs();
		echo $text.self::$_EOL;
	}
	
	static function doctype($ext = "")
	{
		if ($ext == "default") $ext = "PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"";
		
		echo "<!DOCTYPE html {$ext}>".self::$_EOL;
	}
	
	static function html_()
	{
		self::render_tabs(1);
		
		$attrs = func_get_args();

		echo "<html";
		
		if ($attrs[0] == "default") echo " xmlns=\"http://www.w3.org/1999/xhtml\"";
		else self::render_attrs($attrs[0]);

		echo ">".self::$_EOL;
	}
	
	static function _html()
	{
		self::render_tabs(-1);
		
		echo "</html>".self::$_EOL;
	}
	
	static function meta()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<meta";
		self::render_attrs($attrs[0]);
		echo " />".self::$_EOL;
	}
	
	static function link()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<link";
		self::render_attrs($attrs[0]);
		echo " />".self::$_EOL;
	}
	
	static function link_css()
	{
		$attrs = func_get_args();
		
		if (self::$_ccs_dir && !is_array($attrs[0]))
		{
			foreach($attrs as $attr)
			{
				self::render_tabs();
				echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".self::$_ccs_dir.$attr.".css\" />".self::$_EOL;
			}
		}
		else
		{
			self::render_tabs();
			echo "<link rel=\"stylesheet\" type=\"text/css\"";
			self::render_attrs($attrs[0]);
			echo " />".self::$_EOL;
		}
	}
	
	static function script_js()
	{
		$attrs = func_get_args();
		
		if (self::$_js_dir && !is_array($attrs[0]))
		{
			foreach($attrs as $attr)
			{
				self::render_tabs();
				echo "<script type=\"text/javascript\" language=\"javascript\" src=\"".self::$_js_dir.$attr.".js\"></script>".self::$_EOL;
			}
		}
		else
		{
			self::render_tabs();
			echo "<script type=\"text/javascript\" language=\"javascript\"";
			self::render_attrs($attrs[0]);
			echo "></script>".self::$_EOL;
		}
	}
	
	static function script_js_body($body)
	{
		self::render_tabs();
		echo "<script type=\"text/javascript\" language=\"javascript\">".self::$_EOL;
		echo "<!--".self::$_EOL;
		echo $body.self::$_EOL;
		echo "//-->".self::$_EOL;
		
		self::render_tabs();		
		echo "</script>".self::$_EOL;
	}
	
	static function select()
	{
		self::render_tabs(1);
		
		$attrs = func_get_args();
		
		echo "<select";
		self::render_attrs($attrs[0]);
		echo ">".self::$_EOL;
		
		unset($attrs[0]);
		foreach($attrs as $attr)
		{
			if (isset($attr["value"]))
			{
				self::option(array("value" => $attr["value"]), $attr["name"]);
			}
			else
			{
				foreach($attr as $item)
				{
					self::option(array("value" => $item["value"]), $item["name"]);
				}
			}
		}

		self::render_tabs(-1);
		echo "</select>".self::$_EOL;		
	}
	
	static function option()
	{
		self::render_tabs();
		
		$attrs = func_get_args();
		
		echo "<option";
		self::render_attrs($attrs[0]);
		echo ">$attrs[1]</a>".self::$_EOL;
	}
}

class xm extends ExtendedHtmlRendering {}

?>
